package com.weather.pm.adapter;

import java.util.ArrayList;

import com.weather.pm.R;
import com.weather.pm.activity.WeatherDetailActivity;
import com.weather.pm.beans.OrderCPI;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class OrderCPIAdapter extends BaseAdapter {

	private ArrayList<OrderCPI> cpis;
	private Context context;
	private LayoutInflater inflater;
	
	public OrderCPIAdapter(Context context){
		this.context = context;
		inflater = LayoutInflater.from(context);
	}
	
	public void setData(ArrayList<OrderCPI> appInfos){
		this.cpis = appInfos;
	}
	
	@Override
	public int getCount() {
		return cpis.size();
	}

	@Override
	public Object getItem(int position) {
		return cpis.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final OrderCPI info = cpis.get(position);
		Holder holder;
		if(convertView == null){
			convertView = inflater.inflate(R.layout.order_cpi_adapter, null);
			holder = new Holder();
			holder.rightBackground = (RelativeLayout)convertView.findViewById(R.id.rightBackground);
			holder.number = (TextView)convertView.findViewById(R.id.number);
			holder.city = (TextView)convertView.findViewById(R.id.city);
			holder.cpi = (TextView)convertView.findViewById(R.id.cpi);
			convertView.setTag(holder);
		}else{
			holder = (Holder)convertView.getTag();
		}
		if("1".equals(info.number)){
			holder.number.setTextColor(context.getResources().getColor(R.color.firstcolor));
		}else if("2".equals(info.number)){
			holder.number.setTextColor(context.getResources().getColor(R.color.secondcolor));
		}else if("3".equals(info.number)){
			holder.number.setTextColor(context.getResources().getColor(R.color.thirdcolor));
		}else{
			holder.number.setTextColor(context.getResources().getColor(R.color.fourthcolor));
		}
		holder.number.setText(info.number);
		holder.city.setText(info.city);
		holder.cpi.setText(info.cpi);
		showZhishuBg(info.cpi,holder.rightBackground);
		convertView.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent();
				intent.putExtra("id", info.id);
				intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				intent.setClass(context, WeatherDetailActivity.class);
				context.startActivity(intent);
			}
		});
		return convertView;
	}
	
	private void showZhishuBg(String cpi,RelativeLayout rightBackground){
		int icpi = Integer.parseInt(cpi);
		if(icpi>=0 && icpi<=50){
			rightBackground.setBackgroundResource(R.drawable.icon_zhishu01);
		}else if(icpi>50 && icpi <=100){
			rightBackground.setBackgroundResource(R.drawable.icon_zhishu02);
		}else if(icpi>100 && icpi <=150){
			rightBackground.setBackgroundResource(R.drawable.icon_zhishu03);
		}else if(icpi>150 && icpi <=200){
			rightBackground.setBackgroundResource(R.drawable.icon_zhishu04);
		}else if(icpi>200 && icpi <=300){
			rightBackground.setBackgroundResource(R.drawable.icon_zhishu05);
		}else if(icpi>300 && icpi <=500){
			rightBackground.setBackgroundResource(R.drawable.icon_zhishu06);
		}else{
			rightBackground.setBackgroundResource(R.drawable.icon_zhishu07);
		}
	}
	
	private class Holder{
		RelativeLayout rightBackground;
		TextView number;
		TextView city;
		TextView cpi;
	}

}
